package com.sort;

import java.util.Random;

public class Test_1 {
    public static void main(String[] args) {
        Random random = new Random();
        int arr[]=new int[1_0000000];
        for(int i=0;i<arr.length;i++){
            arr[i]= random.nextInt();
        }
        //排序比速度
        //选择排序

//        long pick1=System.currentTimeMillis();
//        A.picksort(arr);
//        long pick2=System.currentTimeMillis();
//        System.out.println("选择排序用时："+(pick2-pick1));
        //快速排序
        for(int i=0;i<arr.length;i++){
            arr[i]= random.nextInt();
        }
        long quick1=System.currentTimeMillis();
        A.quicksort(arr,0,arr.length-1);
        long quick2=System.currentTimeMillis();
        System.out.println("快速排序用时："+(quick2-quick1));

        //归并排序
        for(int i=0;i<arr.length;i++){
            arr[i]= random.nextInt();
        }
        long merge1=System.currentTimeMillis();
        A.mergesort(arr,0,arr.length-1);
        long merge2=System.currentTimeMillis();
        System.out.println("归并排序用时："+(merge2-merge1));


    }
}

class A{
    public static void  picksort(int[] arr){
        for(int i=0;i<arr.length-1;i++){
            int min=arr[i];
            int index=i;
            for(int j=i+1;j<arr.length;j++){
                if(arr[j]<min){
                    min=arr[j];
                    index=j;
                }
            }
            if(index!=i){
                arr[index]=arr[i];
                arr[i]=min;
            }
        }
    }
    //快速排序
    public static void quicksort(int[]arr,int left,int right){
        if(left>=right){
            return;
        }
        int par=partiton(arr,left,right);
        quicksort(arr,left,par-1);
        quicksort(arr,par+1,right);
    }
    public static int partiton(int[]arr,int start,int end){
        int pivot=arr[start];
        int k=start;
        while(start<end){
            while(start<end&&arr[end]>=pivot){
                end--;
            }
            while(start<end&&arr[start]<=pivot){
                start++;
            }
            //交换数据
            swap(arr,start,end);
        }
        swap(arr,k,start);
        return start;
    }

    public static void swap(int[]arr,int i,int j){
        int tem=arr[i];
        arr[i]=arr[j];
        arr[j]=tem;
    }
    //归并排序
    public static void mergesort(int[]arr,int left,int right){
        if(left==right){
            return;
        }
        //分治
        int mid=(left+right)/2;
        mergesort(arr,left,mid);
        mergesort(arr,mid+1,right);
        //合并
        merge(arr,left,right,mid);
    }
    public static void merge(int[]arr,int left,int right,int mid){
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        //
        int k=0;
        int[] temarr=new int[right-left+1];
        while(s1<e1&&s2<e2){
            if(arr[s1]<=arr[s2]){
                temarr[k++]=arr[s1++];
            }
            else{
                temarr[k++]=arr[s2++];
            }
        }
        //查s1
        while(s1<e1){
            temarr[k++]=arr[s1++];
        }
        while(s2<e2){
            temarr[k++]=arr[s2++];
        }
        //把临时数组传给arr
        for(int i=0;i<temarr.length;i++){
            arr[i+left]=temarr[i];
        }
    }

}